package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.Deblocking;
import com.itheima.domain.db.Freeze;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.*;
import com.itheima.service.db.DeblockingService;
import com.itheima.service.db.FreezeService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class UserManager {

    @Reference
    private UserInfoService userInfoService;

    /*
     *
     *  分页查询用户信息
     * @param page  分页数据
     * @param pagesize 分页数据
     * @author
     * @date
     * @return com.itheima.domain.vo.PageBeanVo
     */

    public PageBeanVo findUsers(Integer page, Integer pagesize) {

        PageBeanVo pageBeanVo = userInfoService.findUsersByPage(page, pagesize);

        List<UserInfo> items = (List<UserInfo>) pageBeanVo.getItems();

        //UserInfoAdminVo对象
        List<UserInfoAdminVo> userInfoAdminVoList=new ArrayList<>();

        //判断items是否为空
        if(CollectionUtil.isNotEmpty(items)){
            for (UserInfo userInfo : items) {
                //用户账号
                User user = userService.findById(userInfo.getId());
                //Vo
                UserInfoAdminVo userInfoAdminVo = new UserInfoAdminVo();

                BeanUtil.copyProperties(userInfo,userInfoAdminVo);
                BeanUtil.copyProperties(user,userInfoAdminVo);


                userInfoAdminVo.setUserStatus(userInfo.getStatus()+"");

                userInfoAdminVoList.add(userInfoAdminVo);



            }
        }

        pageBeanVo.setItems(userInfoAdminVoList);
        return pageBeanVo;
    }

    /*
     *
     *  根据Id查询用户详细信息
     * @param userId 用户Id
     * @author
     * @date
     * @return com.itheima.domain.db.UserInfo
     */

    @Reference
    private UserService userService;

    public UserInfoAdminVo findUserById(Long userId) {


        //用户详细信息
        UserInfo userInfo = userInfoService.findById(userId);
        //用户名
        User user=userService.findById(userId);

        //封装vo
        UserInfoAdminVo userInfoAdminVo = new UserInfoAdminVo();

        BeanUtil.copyProperties(userInfo,userInfoAdminVo);
        BeanUtil.copyProperties(user,userInfoAdminVo);

        //用户状态
        userInfoAdminVo.setUserStatus(userInfo.getStatus()+"");

        //用户婚姻状态
        userInfoAdminVo.setMarriage(userInfo.getMarriage());



        return userInfoAdminVo;
    }

    @Reference
    private VideoService videoService;
    /**
     * 根据用户的id 查询视频数据的分页
     * @param uid
     * @param page
     * @param pagesize
     * @return
     */
    public PageBeanVo findVideoByUid(Long uid, Integer page, Integer pagesize) {
        //1.查询数据
        PageBeanVo pageBeanVo = videoService.findVideoByUid(page, pagesize , uid);
        //2.转换数据
        List<Video> items = (List<Video>) pageBeanVo.getItems();
        //准备集合
        List<VideoManagerVo> list = new ArrayList<>();
        //3.1 查询用户数据 查询一次即可
        UserInfo userInfo = userInfoService.findById(uid);
        if(CollectionUtil.isNotEmpty(items)){
            for (Video video : items) {
                //3.2 创建对象
                VideoManagerVo vo = new VideoManagerVo();
                //3.3 封装数据 - 基础数据
                BeanUtil.copyProperties(userInfo , vo);
                BeanUtil.copyProperties(video , vo);
                //3.4 不一样的数据 处理日期(新加了一个vo对象)
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = simpleDateFormat.format(new Date(video.getCreated()));
                vo.setCreateDate(date);
                vo.setSignature("左手右手一个慢动作~~");
                list.add(vo);
            }
        }
        pageBeanVo.setItems(list);
        return pageBeanVo;
    }



    @Reference
    private MovementService movementService;
    /**
     * 查询个人动态
     * @param uid
     * @param intState
     * @param page
     * @param pagesize
     * @return
     */
    public PageBeanVo findByMessages(Long uid, Integer intState, Integer page, Integer pagesize) {
        //查询分页数据
        PageBeanVo pageBeanVo = movementService.findByMessages(uid , intState , page, pagesize);

        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        List<MovementVo> movementVoList = new ArrayList<>();
        //转换
        if(CollectionUtil.isNotEmpty(movementList)){
            for (Movement movement : movementList) {
                MovementVo vo = new MovementVo();
                UserInfo userInfo = userInfoService.findById(movement.getUserId());
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);
                //处理时间 api有坑 我自己处理时间即可
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(movement.getCreated())));
                movementVoList.add(vo);
            }
        }


        pageBeanVo.setItems(movementVoList);
        return pageBeanVo;
    }

    //查询单条动态
    public MovementVo findMovementById(String movementId) {
        Movement movement = movementService.findMovementByMovementId(movementId);

        MovementVo vo = new MovementVo();
        UserInfo userInfo = userInfoService.findById(movement.getUserId());
        vo.setUserInfo(userInfo);
        vo.setMovement(movement);
        //处理时间 api有坑 我自己处理时间即可
        vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(movement.getCreated())));
        return vo;
    }

    @Reference
    private CommentService commentService;
    //查询评论数据
    public PageBeanVo findCommentByMovementId(Integer page, Integer pagesize, String messageID) {
        //1.查询评论的所有数据
        PageBeanVo pageBeanVo = commentService.findCommentList(page, pagesize, messageID);
        //2.转换数据
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        List<CommentVo> voList = new ArrayList<>();
        //3.转换
        if(CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                CommentVo vo = new CommentVo();
                BeanUtil.copyProperties( comment , vo );
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date( comment.getCreated() )));
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }
    /*
     *
     *  冻结用户
     * @param restmap
     * @author
     * @date
     * @return java.lang.String
     */

    @Reference
    private FreezeService freezeService;

    public String saveUserFreeze(Map restmap) {

        //前台接受的数据
        String message="";
        //用户Id
        String userId = restmap.get("userId").toString();

        //冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
        String strfreezingTime = restmap.get("freezingTime").toString();
        Integer freezingTime = Integer.valueOf(strfreezingTime);


        //冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
        String strfreezingRange = restmap.get("freezingRange").toString();
        Integer freezingRange = Integer.valueOf(strfreezingRange);


        //冻结原因
        String reasonsForFreezing = restmap.get("reasonsForFreezing").toString();
        //冻结备注
        String frozenRemarks = restmap.get("frozenRemarks").toString();

        Freeze freeze = new Freeze();
        //用户Id
        freeze.setUserId(Long.valueOf(userId));
        //用户冻结时间
        freeze.setFreezingTime(freezingTime);
        //用户冻结类型
        freeze.setFreezingRange(freezingRange);
        //用户冻结原因
        freeze.setCause(reasonsForFreezing);
        //用户冻结备注
        freeze.setRemark(frozenRemarks);


        if (freezeService.saveUserFreeze(freeze)>0){

            //查询出冻结用户 修改冻结状态
            UserInfo userInfo = userInfoService.findById(Long.valueOf(userId));
            userInfo.setStatus(2);
            userInfoService.update(userInfo);

            message="操作成功";
        }

        return message;
    }

    @Reference
    private DeblockingService deblockingService;
    /*
     *
     *  用户解冻
     * @param restmap   用户Id和解冻原因
     * @author
     * @date
     * @return java.util.Map
     */

    public String saveuserDeblocking(Map restmap) {

        //前台接受的数据
        String message="";

        //用户Id
        String userId = restmap.get("userId").toString();

        //解冻原因
        String reasonsForThawing = restmap.get("frozenRemarks").toString();

        Deblocking deblocking = new Deblocking();
        //用户Id
        deblocking.setUserId(Long.valueOf(userId));
        //解冻原因
        deblocking.setReasonsForThawing(reasonsForThawing);




        if (deblockingService.saveuserDeblocking(deblocking)>0){

            freezeService.deleteUserFreeze(Integer.valueOf(userId));

            //查询出冻结用户 修改冻结状态
            UserInfo userInfo = userInfoService.findById(Long.valueOf(userId));
            userInfo.setStatus(1);
            userInfoService.update(userInfo);

            message="操作成功";
        }

        return  message;
    }
}
